package redarrow.wb.dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import redarrow.wb.util.DBConfig;
import redarrow.wb.util.IConstants;
import redarrow.wb.util.Logger;

public class UserDao {

	public static DBConfig dbc = new DBConfig();

	public static int InsertUser(String username, String password,
			String userGenDate, String status) {
		// TODO Auto-generated method stub
		try {
			dbc.loadProperties();
			String url = DBConfig.db_url;
			String account = DBConfig.db_account;
			String pass = DBConfig.db_password;
			String insert = "INSERT INTO tbl_user(username, password, user_gendate, status)"
					+ "VALUES (?, ?, ?, ?)";
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(url, account, pass);
			PreparedStatement ps = con.prepareStatement(insert);
			ps.setString(1, username);
			ps.setString(2, password);
			ps.setString(3, userGenDate);
			ps.setString(4, status);
			ps.executeUpdate();
			con.close();

		} catch (SQLException ex) {
			//ex.printStackTrace();
			return IConstants.SQLException;

		} catch (ClassNotFoundException e) {

			return IConstants.CLASS_NOT_FOUND;
			// e.printStackTrace();
		} catch (IOException e) {

			return IConstants.IOException;
			// e.printStackTrace();
		}
		return IConstants.SUCCESS;
	}

	public static Boolean checkUserExist(String username) throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		int number = 0;

		dbc.loadProperties();
		String url = DBConfig.db_url;
		String account = DBConfig.db_account;
		String pass = DBConfig.db_password;
		String sql = "SELECT COUNT(*) AS total FROM tbl_user WHERE username ='"
				+ username + "'";
		Class.forName("com.mysql.jdbc.Driver");
		Connection con = DriverManager.getConnection(url, account, pass);
		Statement stmt = con.createStatement();
		ResultSet result = stmt.executeQuery(sql);
		result.next();
		number = result.getInt("total");

		if (number == 0)
			return false;
		else
			return true;
	}

	public static int getUserFromUsername(String username) throws IOException,
			ClassNotFoundException, SQLException {
		// TODO Auto-generated method stub
		int userId = 0;
		Boolean foo = checkUserExist(username);
		if (foo) {
			dbc.loadProperties();
			String url = DBConfig.db_url;
			String account = DBConfig.db_account;
			String pass = DBConfig.db_password;
			String sql = "SELECT user_id FROM tbl_user WHERE username = '"
					+ username + "'";
			Class.forName("com.mysql.jdbc.Driver");
			Connection con = DriverManager.getConnection(url, account, pass);
			Statement stmt = con.createStatement();
			ResultSet result = stmt.executeQuery(sql);
			while (result.next()) {
				userId = result.getInt("user_id");
			}
			return userId;
		} else {
			return IConstants.USER_NOT_EXIST;
		}

	}

}
